package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.ComOrganizationDepartmentesUsers;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesUsersAddParam;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesUsersQueryParam;
import com.hoshiicloud.user.param.organization.ComOrganizationDepartmentesUsersUpdateParam;
import com.hoshiicloud.user.service.organization.ComOrganizationDepartmentesUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 租户机构部门人员 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-27
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/organization/com-organization-departmentes-users")
@Api(value = "SAAS租户供应商工作人员", tags = {"SAAS租户供应商工作人员"})
@ApiSort(13)
public class ComOrganizationDepartmentesUsersController {
    @Autowired
    private ComOrganizationDepartmentesUsersService comOrganizationDepartmentesUsersService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询租户机构部门人员
    */
    @ApiOperation(value = "分页查询租户机构部门人员")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ComOrganizationDepartmentesUsersQueryParam param) {
        try {
            log.info("分页查询租户机构部门人员");
            QueryWrapper<ComOrganizationDepartmentesUsers> queryWrapper = new QueryWrapper<ComOrganizationDepartmentesUsers>();
            queryWrapper.eq(param.getCompanyId() != null, "company_id", param.getCompanyId());
            queryWrapper.eq(param.getDepartmentId() != null, "department_id", param.getDepartmentId());
            queryWrapper.eq(param.getUserId() != null, "user_id", param.getUserId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRole()), "role", param.getRole());
            queryWrapper.eq(param.getEnabled() != null, "enabled", param.getEnabled());
            Page<ComOrganizationDepartmentesUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(comOrganizationDepartmentesUsersService.getPage(page, queryWrapper, key));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询租户机构部门人员异常", e);
            return Response.failed("分页查询租户机构部门人员异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询租户机构部门人员
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询租户机构部门人员")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(comOrganizationDepartmentesUsersService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增租户机构部门人员")
    public Response save(@RequestBody @Valid ComOrganizationDepartmentesUsersAddParam param) {
        ComOrganizationDepartmentesUsers data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationDepartmentesUsers.class);
        }catch (Exception e) {
            log.error("新增租户机构部门人员Bean转换异常", e);
            return Response.failed("新增租户机构部门人员Bean转换异常");
        }
        return Response.data(comOrganizationDepartmentesUsersService.save(data));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 修改租户机构部门人员
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改租户机构部门人员")
    public Response update(@RequestBody @Valid ComOrganizationDepartmentesUsersUpdateParam param) {
        ComOrganizationDepartmentesUsers data;
        try {
            data = BeanUtils.covertType(param, ComOrganizationDepartmentesUsers.class);
        }catch (Exception e) {
            log.error("修改租户机构部门人员异常Bean转换异常", e);
            return Response.failed("修改租户机构部门人员异常Bean转换异常");
        }
        return Response.data(comOrganizationDepartmentesUsersService.updateById(data));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除租户机构部门人员
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除租户机构部门人员")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        if(comOrganizationDepartmentesUsersService.deleteById(id)){
            return Response.data("删除租户机构部门人员成功");
        }else{
            return Response.data("删除租户机构部门人员失败");
        }

    }

    /**
    * @Author:campchen
    * @Description: StringUtils.join(ids, "") 为缓存中添加唯一KEY  如不合适可选择更好方法
    * @Date:上午 10:18 2019/5/11
    * @Param:  ids
    * @return: com.hoshiicloud.common.rpc.Response
    */
    @ApiOperation(value = "根据id集合查询租户机构部门人员")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if(CollectionUtils.isNotEmpty(ids)){
            ids.sort(null);
            return Response.data(comOrganizationDepartmentesUsersService.getByIds(ids,StringUtils.join(ids, "")));
        }else{
            return  Response.data(null);
        }
    }


    }
